package cn.tedu.csmall.stock.webapi.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitConfig {
    //定义队列、交换机、路由键的常量
    public static final String STOCK_QUEUE = "stock_queue";
    public static final String STOCK_EXCHANGE = "stock_exchange";
    public static final String STOCK_ROUTE = "stock_route";

    //创建队列，保存到spring中
    @Bean
    public Queue stockQueue(){
        return new Queue(STOCK_QUEUE);
    }
    //创建交换机，保存到spring中
    @Bean
    public DirectExchange stockDirectExchange(){
        return new DirectExchange(STOCK_EXCHANGE);
    }
    //绑定队列和交换机
    @Bean
    public Binding stockBinding(){
        return BindingBuilder.bind(stockQueue()).to(stockDirectExchange()).with(STOCK_ROUTE);
    }
}
